#include<iostream>
#include<unistd.h>

#include<ctime>

int gval = 100;
//新线程
void *threadStart(void *args)
{
    //新线程
    while (true)
    {
        sleep(1);

        std::cout << "new thread running..." << ", pid:  " << getpid() <<
         " gval: " << gval << " &gval: " << &gval << std::endl;


        // int x = rand() % 15;
        // std::cout << "new thread running..." << ", pid:  " << getpid() << ", x = " << x << std::endl;

        // if(x == 0)
        // {
        //     int *p = nullptr; //故意写一个野指针
        //     *p = 100;
        // }
    }
}

int main()
{
    srand(time(nullptr));
     
    pthread_t tid1;
    pthread_create(&tid1, nullptr, threadStart, (void*)"thread-new");
    
    pthread_t tid2;
    pthread_create(&tid2, nullptr, threadStart, (void*)"thread-new");    

    pthread_t tid3;
    pthread_create(&tid3, nullptr, threadStart, (void*)"thread-new");
    //主线程
    while (true)
    {
        sleep(1);
        std::cout << "main thread running..." << ", pid:  " << getpid() <<
        " gval: " << gval << " &gval: " << &gval << std::endl;
        gval++;//主线程修改变量值
    }
    return 0;
}